var express = require('express');
var router = express.Router();
var uploadFile = require("../config/uploadFile");
var userModel = require("../models/userModel");
var path = require('path');
var formidable = require('formidable');
var gm = require('gm');

//文件上传-页面
router.get("/fileUpload",function(req,res) {
    res.render('./util/fileUpload',{username:req.session.username,userid:req.session.userid,tempName:"fileUpload"});
});

//文件上传的操作
router.post('/file',/* upload.single('file'), */function (req,res) {
    
    var savePath = "./uploads";
    var fileMaxSize = 1024*400;
    var allowTypes = ['image/jpeg','image/jpg','image/png','image/gif'];
    var upload = uploadFile(savePath,allowTypes,fileMaxSize);
    var uploader = upload.upload.single('file');
    var file = req.files;
    //console.log(file);
    var filename = upload.filename;
	
    uploader(req,res,function (err) {
        //console.log("file:"+req.file);
        //console.log(err);
        if(err){
            //console.log(err.code);
            //防止程序继续执行
            switch(err.code){
                case 'LIMIT_FILE_SIZE':
                    req.flash('errMsg','file size too large.');
                    break;
                case 'FILE_TYPE_ERROR':
                    req.flash('errMsg','file type not allowed.');
                    break;
            }
            //跳转回首页
            //res.redirect('/');
            //跳转回原页面
            res.redirect('back');
            
        }else{
            //console.log(req.file);
            //console.log(req.body);
            //res.send("file upload success, file: "+req.file.filename)
            res.redirect('back');
        }
    });
});

//用户上传头像操作
router.post('/uploadAvatar/:_id',function (req,res) {
    var userid = req.params._id;
    //console.log('req user id: '+userid);
    var savePath = "./uploads/avatars";
    var fileMaxSize = 1024*400; //文件大小限制
    var allowTypes = ['image/jpeg','image/jpg','image/png','image/gif'];
    var upload = uploadFile(savePath,allowTypes,fileMaxSize);
    var uploader = upload.upload.single('file');
    var file = req.files;
    //console.log(file);
    var filename = upload.filename;


    uploader(req,res,function (err) {
        //console.log("file:"+req.file);
        //console.log(err);
        if(err){
            //console.log(err.code);
            //防止程序继续执行
            switch(err.code){
                case 'LIMIT_FILE_SIZE':
                    req.flash('errMsg','file size too large.');
                    break;
                case 'FILE_TYPE_ERROR':
                    req.flash('errMsg','file type not allowed.');
                    break;
            }
            //跳转回首页
            //res.redirect('/');
            //跳转回原页面
            

            res.redirect('back');
            
        }else{
            //console.log(req.file);
            //console.log(req.body);
            //res.send("file upload success, file: "+req.file.filename)
            //onsole.log(filename+path.extname(req.file.originalname));
            //console.log("userid: "+req.params._id);
            console.log(req.file.path);
            var con = {
                _id: userid
                //avatar:'/avatars/'+filename+path.extname(req.file.originalname)
            };
            var newData = {
                avatar:'/avatars/'+filename+path.extname(req.file.originalname)
            };
            //缩放上传上来的头像, 并将头像图片路径保存
            gm(req.file.path).resize(200,200).write(req.file.path,function(err) {
                console.log(err);     
                userModel.update(con,newData,function(err,msg){
                    if(err){
                        console.log(err);
                        return;
                    }
                    res.redirect('back');
                },{ upsert: true });      
            });
        }
    });

    
});

module.exports = router;